/**
 * 
 */
package org.hackystat.ui.sensordataviewer.server;

import java.util.ArrayList;

/**
 * This class embodies an ordered list of SDVEvents.
 * @author David J. Nickles
 *
 */
public class EventCollection {
  
  private ArrayList<Event> eventCollection;

  /**
   * Initializes a SDVEventColelction for classifying and storing SDVEvents.
   */
  private final void init() {
    this.eventCollection = new ArrayList<Event>();
  }

  /**
   * Creates a SDVEventColelction for classifying and storing SDVEvents.
   */
  public EventCollection() {
    init();
  }
  
  /**
   * This method adds the sensor data element to this event.
   * @param item a sensor data element
   */
  public void add(SensorDataItem item) {
    for (Event event : this.eventCollection) {
      if (event.acceptsThis(item)) {
        event.add(item);
        return;
      }
    }
    this.eventCollection.add(new Event(item));
  }
  
  /**
   * Gets the count of sensor data instances represented by this series of events.
   * @return the total sensor data instances represented by this series of events.
   */
  public int size() {
    int s = 0;
    for (Event e : this.eventCollection) {
      s += e.size();
    }
    return s;
  }
  
  /**
   * Gets a Tree populated with the values of this SDVEvent.
   * @return TreeItem populated with the values of this SDVEvent
   */
  public ArrayList<ArrayList<String>> getList() {
    ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
    for (Event event : this.eventCollection) {
      ArrayList<String> sublist = new ArrayList<String>();
      sublist.add(event.toString());
      sublist.add(Integer.toString(event.size()));
      sublist.add(Long.toString(event.getID()));
      for (String href : event.getChildren()) {
        sublist.add(href);
      }
      list.add(sublist);
    }
    return list;
  }
}